package com.mchsdk.paysdk.activity;

import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.mchsdk.paysdk.bean.PersonalCenterModel;
import com.mchsdk.paysdk.callback.PlatformRegisterCallback;
import com.mchsdk.paysdk.common.Constant;
import com.mchsdk.paysdk.dialog.MCTipDialog;
import com.mchsdk.paysdk.entity.ChannelAndGameinfo;
//import com.mchsdk.paysdk.http.process.SetUserNameProcess;
import com.mchsdk.paysdk.http.process.UpdateNikeNameProcess;
import com.mchsdk.paysdk.utils.ToastUtil;

public class MCSetUserNameActivity extends MCBaseActivity {
    //private String TAG = "MCSetUserNameActivity";
    private EditText userName,userPwd;
    private Button btnOk;
    private ImageView mBack;
    private String name,pwd;


    /**
     * 日志打印
     */
   // private final static String TAG = "MCPersonalInfoActivity";

    /**
     * 显示用户昵称部分
     */
    RelativeLayout rlCurrentNike;

    /**
     * 修改用户昵称部分
     */
    RelativeLayout rlUpdateNike;

    /**
     * 昵称输入框
     */
    EditText etUpdateNike;

    /**
     * 昵称
     */
    TextView txtNike;

    /**
     * 用户名
     */
    TextView txtAccount;

    /**
     * 平台币余额
     */
    TextView txtPtbMoney;

    /**
     * 绑定手机状态
     */
    TextView txtBindPhoneState;

    /**
     * 绑定平台币余额
     */
    TextView txtBindPtbMoney;

    /**
     * 退出登录
     */
    Button btn_perifo_ext;
    /**
     * 键盘控制
     */
    InputMethodManager imm;

    /**
     * 用户名
     */
    private String account;

    /**
     * 昵称
     */
    String nickName;

    MCTipDialog nickTip;

    private PlatformRegisterCallback mQuickRegisterCallback;
    /**
     * 日志打印
     */
    private static final String TAG = "PlatformQuickRegisterDialog";

    /**
     * 用户名
     **/
    protected static final String KEY_ACCOUNT = "mc_account";

    /**
     * 密码
     **/
    protected static final String KEY_PASSWORD = "mc_password";

    private MCTipDialog infoTip;

    private Handler mHandler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (null != infoTip) {
                try {
                    infoTip.dismiss();
                    infoTip = null;
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            try {
                if (null != nickTip) {
                    nickTip.dismiss();
                    nickTip = null;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            switch (msg.what) {
                case Constant.GET_USER_INFO_SUCCESS:// 获取用户信息
                    ChannelAndGameinfo info = (ChannelAndGameinfo) msg.obj;
                    if (null != info) {
                        handlerUserInfo(info);
                    } else {
                        Toast.makeText(getApplicationContext(), "请重新登录", Toast.LENGTH_SHORT).show();
                        finish();
                    }
                    break;
                case Constant.GET_USER_INFO_FAIL:
                    String tip = (String) msg.obj;
                    Toast.makeText(getApplicationContext(), tip, Toast.LENGTH_SHORT).show();
                    finish();
                    break;
                case Constant.UPDATE_NIKE_SUCCESS:
                    handlerNikeName();
                    break;
                case Constant.UPDATE_NIKE_FAIL:
                    String res = (String) msg.obj;
                    if (TextUtils.isEmpty(res)) {
                        res = "网络异常";
                    }
                    Toast.makeText(getApplicationContext(), res, Toast.LENGTH_SHORT).show();
                    break;
                default:
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(getLayout("activity_mch_personal_info_setuser"));

        initView();


    }


    protected void handlerUserInfo(ChannelAndGameinfo info) {
        // MCLog.e(TAG, "[handlerUserInfo] info:" + info.toString());
        PersonalCenterModel.getInstance().channelAndGame.setNikeName(info.getNikeName());
        PersonalCenterModel.getInstance().channelAndGame.setPlatformMoney(info.getPlatformMoney());
        PersonalCenterModel.getInstance().channelAndGame.setPhoneNumber(info.getPhoneNumber());
        PersonalCenterModel.getInstance().channelAndGame.setBindPtbMoney(info.getBindPtbMoney());

        txtAccount.setText("账号：" + account);
        String nick = info.getNikeName();
        if (TextUtils.isEmpty(nick)) {
            nick = "梦创sdk";
        }
        txtNike.setText(nick);
        txtPtbMoney.setText(String.format("%.2f", info.getPlatformMoney()));
        txtBindPtbMoney.setText(String.format("%.2f", info.getBindPtbMoney()));

        isBindPhone();
    }


    protected void handlerNikeName() {
        if (!TextUtils.isEmpty(nickName)) {
            txtNike.setText(nickName);
            etUpdateNike.setText("");

            rlCurrentNike.setVisibility(View.VISIBLE);
            rlUpdateNike.setVisibility(View.GONE);
        }
    }


    /**
     * 是否绑定手机号
     */
    private void isBindPhone() {
        if (TextUtils.isEmpty(PersonalCenterModel.getInstance().getPhone())) {
            txtBindPhoneState.setText("未设置");
        } else {
            txtBindPhoneState.setText("已设置");
        }
    }
//    private void ititData() {
//
//
//        String regular = "^[a-zA-Z0-9]{6,15}$";
//
//        if(TextUtils.isEmpty(name)||TextUtils.isEmpty(pwd)){
//            ToastUtil.show(this, "用户名和密码不能为空");
//            return;
//        }
//
//        if (!name.matches(regular)) {// 改为正则表达式
//            ToastUtil.show(this, "请输入6—15数数字或字母");
//            return;
//        }
//        if (!pwd.matches(regular)) {// 改为正则表达式
//            ToastUtil.show(this, "密码格式不正确");
//            return;
//        }
//
//    }
    View.OnClickListener updateNikeClick = new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            //imm.hideSoftInputFromWindow(userName.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
            name = userName.getText().toString().trim();
            if (TextUtils.isEmpty(name)) {
                Toast.makeText(getApplicationContext(), "请输入昵称", Toast.LENGTH_SHORT).show();

                return;
            }
            nickTip = new MCTipDialog.Builder().setMessage("正在修改昵称").show(MCSetUserNameActivity.this, getFragmentManager());

            UpdateNikeNameProcess updateNickPreocess = new UpdateNikeNameProcess();
            updateNickPreocess.setNikeName(name);
            updateNickPreocess.setCode("nickname");
            updateNickPreocess.post(mHandler);
            ToastUtil.show(MCSetUserNameActivity.this, "用户名修改成功");
        }
    };

//    private void changePwd() {
////		RequestParams params = createParams(senAcc, newPwd_);
////		ChangePasswordRequest changeRequest = new ChangePasswordRequest(myHandler);
////		changeRequest.post(myHandler);
////
////        SetUserNameProcess setUserNameProcess = new SetUserNameProcess();
////        setUserNameProcess.setPwd(pwd);
////
////        setUserNameProcess.setCode("pwd");
////        setUserNameProcess.post(myHandler);
//
//
//    }

    private void initView() {

        userName = (EditText) findViewById(getId("id_edit_username"));
        userPwd = (EditText) findViewById(getId("id_user_pwd"));

        btnOk = (Button) findViewById(getId("id_bton_ok"));

        mBack = (ImageView) findViewById(getId("id_iv_close"));
        mBack.setOnClickListener(backClick);
        btnOk.setOnClickListener(updateNikeClick);
    }

    View.OnClickListener backClick = new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            MCSetUserNameActivity.this.finish();
        }
    };

//    View.OnClickListener subListener = new View.OnClickListener() {
//
//        @Override
//        public void onClick(View v) {
//
//            name = userName.getText().toString();
//            pwd = userPwd.getText().toString();
//
//            if (null != mQuickRegisterCallback) {
//                mQuickRegisterCallback.platformRegister(userName
//                        .getText().toString(), userPwd.getText().toString().trim(), userPwd.getText()
//                        .toString(), "", "");
//            }
//
//            ititData();
//        }
//    };


//    public Handler myHandler = new Handler() {
//
//        @Override
//        public void handleMessage(Message msg) {
//            super.handleMessage(msg);
//            switch (msg.what) {
//                case Constant.REGISTER_SUCCESS:// 注册成功修改成功
//                    MCLog.e(TAG, "update pwd onSuccess");
//                    PersonalCenterModel.getInstance().channelAndGame.setPassword(pwd);
//                    PreSharedManager.setString(Constant.PASSWORD, pwd, MCSetUserNameActivity.this);
//                    //修改密码之后不要自动更新
//                    PersonalCenterModel.getInstance().channelAndGame.setPassword(null);
//                    PreSharedManager.setString(Constant.PASSWORD,null, MCSetUserNameActivity.this);
//                    ToastUtil.show(MCSetUserNameActivity.this, "密码修改成功");
//                    MCSetUserNameActivity.this.finish();
//                    break;
//                case Constant.REGISTER_FAIL:// 密码修改失败
//                    String tip = (String)msg.obj;
//                    if (TextUtils.isEmpty(tip)) {
//                        tip = "密码修改失败";
//                    }
//                    ToastUtil.show(MCSetUserNameActivity.this, tip);
//                    break;
//                default:
//                    break;
//            }
//        }
//    };


}
